package com.lab.book.server.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import org.springframework.transaction.interceptor.TransactionInterceptor;

import javax.sql.DataSource;
import java.util.Properties;

@Slf4j
//@Configuration
//@EnableTransactionManagement
public class TransactionConfig implements TransactionManagementConfigurer {

    //@Autowired
    private DataSource dataSource;

    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        log.info(StringUtils.center("Override AnnotationDrivenTransactionManager", 52, "="));
        return txManager();
    }

    //@Bean
    public PlatformTransactionManager txManager() {
        log.info(StringUtils.center("Creating Bean txManager", 52, "="));
        return new DataSourceTransactionManager(dataSource);
    }

    //@Bean
    public TransactionInterceptor transactionInterceptor(PlatformTransactionManager platformTransactionManager) {
        log.info(StringUtils.center("Creating Bean TransactionInterceptor", 52, "="));

        TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
        transactionInterceptor.setTransactionManager(annotationDrivenTransactionManager());
        Properties transactionAttributes = new Properties();

        transactionAttributes.setProperty("insert*", "PROPAGATION_REQUIRED,-Throwable");
        transactionAttributes.setProperty("add*", "PROPAGATION_REQUIRED,-Throwable");
        transactionAttributes.setProperty("update*", "PROPAGATION_REQUIRED,-Throwable");
        transactionAttributes.setProperty("del*", "PROPAGATION_REQUIRED,-Throwable");

        transactionAttributes.setProperty("select*", "PROPAGATION_REQUIRED,-Throwable,readOnly");
        transactionAttributes.setProperty("get*", "PROPAGATION_REQUIRED,-Throwable,readOnly");
        transactionAttributes.setProperty("query*", "PROPAGATION_REQUIRED,-Throwable,readOnly");
        transactionAttributes.setProperty("fetch*", "PROPAGATION_REQUIRED,-Throwable,readOnly");
        transactionAttributes.setProperty("search*", "PROPAGATION_REQUIRED,-Throwable,readOnly");

        transactionInterceptor.setTransactionAttributes(transactionAttributes);
        return transactionInterceptor;
    }

    //@Bean
    public BeanNameAutoProxyCreator transactionAutoProxy() {
        log.info(StringUtils.center("Creating Bean BeanNameAutoProxyCreator", 52, "="));

        BeanNameAutoProxyCreator transactionAutoProxy = new BeanNameAutoProxyCreator();
        transactionAutoProxy.setProxyTargetClass(true);
        transactionAutoProxy.setBeanNames("*Service*");
        transactionAutoProxy.setInterceptorNames("transactionInterceptor");
        return transactionAutoProxy;
    }
}
